package com.eltsistema.filtro;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.hibernate.Session;

import com.eltsistema.hibernate.HibernateUtil;
import com.eltsistemas.jdbc.ConnectionFactory;

public class FiltroConexao implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
			// TODO Auto-generated method stub
		
			Session session = new HibernateUtil().getSession();
			session.beginTransaction();
			
			request.setAttribute("session", session);
		
			chain.doFilter(request, response);
			
			if(session !=null && session.isOpen())
			{
				session.close();
				System.out.println("Conexao fechada");
			}

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
	}

}
